package it.getta.vicinometro;

import it.getta.vicinometro.adapter.FullscreenLazyGalleryAdapter;
import it.getta.vicinometro.resources.ResBundle;
import it.getta.vicinometro.utils.FadingTextView;
import it.getta.vicinometro.utils.FileCache;
import it.getta.vicinometro.utils.SwipeIndicatorGallery;
import it.getta.vicinometro.utils.SwipeIndicatorGallery.OnAttachedToWindow;
import it.getta.vicinometro.utils.Utils;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;

public class FullscreenGalleryActivity extends Activity {

	private FullscreenLazyGalleryAdapter mAdapter;
	private SwipeIndicatorGallery mGallery;
	private FileCache mFileCache;
	private FadingTextView mCounter;
	private Handler mHandler = new Handler();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fullscreen_gallery); 
		
		String[] data = getIntent().getStringArrayExtra(ResBundle.EXTRA_INTENT_IMAGES_URL);
		int selected = getIntent().getIntExtra(ResBundle.EXTRA_INTENT_SELECTED_IMAGE, 0);
		
		if(data == null) {
			finish();
		} else {
			
			int dWidth = Utils.getDisplayWidth(this);
			int dHeight = Utils.getDisplayHeight(this);
			Utils.hardLong("Display w = " + dWidth);
			Utils.hardLong("Display h = " + dHeight);
			
			if(Utils.isLandscape(this)) {
//				dHeight = dHeight - (int)Utils.pxToDip(this, ResBundle.GALLERY_BOTTOM_SPACE);
				dHeight = dHeight -  ResBundle.GALLERY_BOTTOM_SPACE;
			}
			
			FrameLayout.LayoutParams galleryParams = new FrameLayout.LayoutParams(dWidth, dHeight);
			galleryParams.gravity = Gravity.TOP;
			
			mFileCache = new FileCache(this, ResBundle.FULLSCREEN_FILE_CACHE_ROOT);
			
			mCounter = (FadingTextView)findViewById(R.id.counter_text);
			
			mAdapter = new FullscreenLazyGalleryAdapter(this, mFileCache);
			mAdapter.setData(data);
			mAdapter.setCounterTextView(mCounter);
			mGallery = (SwipeIndicatorGallery)findViewById(R.id.fullscreen_gallery); 
			mGallery.setLayoutParams(galleryParams);
			mGallery.setAdapter(mAdapter);
			mGallery.setSelection(selected);
			mGallery.setOnAttachedToWindow(new OnAttachedToWindow() {
				
				@Override
				public void onAttached() {
					showCounter();
				}
			});
			mGallery.setOnTouchListener(new OnTouchListener() {
				
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					if(!mCounter.isFadingIn() && !mCounter.isShown()) {
						showCounter();
					}
					
					return false;
				}
			});
		
		}
		
	}
	
	private synchronized void showCounter() {
		Thread show = new Thread(new Runnable() {
			
			@Override
			public void run() {
				mHandler.post(new Runnable() {
					
					@Override
					public void run() {
						mCounter.fadeIn();
						
					}
				});
				
				try {
					Thread.sleep(2500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				mHandler.post(new Runnable() {
					
					@Override
					public void run() {
						mCounter.fadeOut();
						
					}
				});
				
			}
		});
		show.start();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
//		mFileCache.clear();
	}
	
}
